﻿using System;
using System.Collections.Generic;

using System.Text;

using OpenTK;
using OpenTK.Graphics.OpenGL;
using Xeno.Core;
using Xeno.Module;
using System.Drawing;
using OpenTK;

namespace Xeno.GUI
{
    public class XuiGraphView : XuiWidget
    {
        
        public XenoGraph Graph { get; set; }
        private XenoRenderTarget Target;
        private XenoTexture2D FrameBuffer;
        private XenoTextureDepth DepthBuffer;

        public XuiGraphView(Vector2 size,XenoGraph graph)
            : base(size)
        {
            
            Graph = graph;
            
            Target = new XenoRenderTarget();
            FrameBuffer = new XenoTexture2D((int)XenoCore.DisplayWidth, (int)XenoCore.DisplayHeight);
            DepthBuffer = new XenoTextureDepth((int)XenoCore.DisplayWidth, (int)XenoCore.DisplayHeight);

        }
        public override void DoUpdate()
        {
            Graph.UpdateGraph();
        }
        public override void DoRender()
        {
            XenoCore.LightList = Graph.RootNode.AllWithType(typeof(XenoMod_Light), null);
            Target.Bind();

            Target.AttachTexture(FrameBuffer);
            Target.AttachDepth(DepthBuffer);

            Graph.RootNode.PerformRender();

            Target.Unbind();

         

//            FrameBuffer.Bind(0);
            Graph.FrameBuffer = FrameBuffer;
            XenoTexture2D tex = Graph.RenderGraph();
           

            Begin2D();

            tex.Bind(0);

            Vector3 wp = Attached.LocalPosition;

            GL.Color4(Color.WhiteSmoke);

            GL.Begin(BeginMode.Quads);

            GL.TexCoord2(0, 1);
            GL.Vertex2(wp.X, wp.Y);
            GL.TexCoord2(1, 1);
            GL.Vertex2(wp.X + Size.X, wp.Y);

            GL.Color4(Color.DarkSlateBlue);
            GL.TexCoord2(1, 0);
            GL.Vertex2(wp.X + Size.X, wp.Y + Size.Y);
            GL.TexCoord2(0, 0);
            GL.Vertex2(wp.X, wp.Y + Size.Y);
           


            GL.End();

            GL.LineWidth(1);
            GL.Begin(BeginMode.Lines);

            GL.Color4(Color.DarkSlateGray);

            GL.Vertex2(wp.X, wp.Y);
            GL.Vertex2(wp.X, wp.Y + Size.Y);
            GL.Vertex2(wp.X, wp.Y);
            GL.Vertex2(wp.X + Size.X, wp.Y);


            GL.End();

            tex.Unbind(0);

            End2D();
                
      //      float tx = wp.X + Size.X / 2 - (TextCache.Width / 2);
    //        float ty = 5;
  //          End2D();

//            XenoText.DrawText(TextCache, (int)tx, (int)ty, Color.Black);
        }

    }
}
